package com.baizhi.cmfz.test;

import com.baizhi.cmfz.CmfzApplication;
import com.baizhi.cmfz.service.UserService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Handler;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CmfzApplication.class)
public class testThread {
    @Autowired
    private UserService userService;
    @Test
    public  Map setAllThread()throws InterruptedException{
        Map map=new HashMap<>();
        //创建计数器
        CountDownLatch countDownLatch=new CountDownLatch(3);
        //开启三个任务线程
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Map map1= userService.showDate();
                    map.put("map1",map1);
                    countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                }
            }
        });
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> map2=userService.showCity();
                map.put("map2",map2);
                countDownLatch.countDown();
            }
        });

        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> map3=userService.showSexs();
                map.put("map3",map3);
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        return  map;
    }
}
